import {
  getDictTypeByPage,
  addDictType,
  updateDictType,
  delDictType,
} from '@/services/system/dictType';
import { PlusOutlined } from '@ant-design/icons';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import { Button, Form, Input, message, Modal, Tree } from 'antd';
import { sortTran, filterTran, filterTranNew } from '@/utils/requestUtil';
import React, { useRef, useState } from 'react';
import { NavLink } from 'react-router-dom';
const DictType: React.FC<{}> = () => {
  const actionRef = useRef<ActionType>();
  const [modalVisit, setModalVisit] = useState(false); // 新建和编辑提示框
  const [confirmLoading, setConfirmLoading] = useState(false); //编辑框的disable
  const [formTitle, setFormTitle] = useState('新建'); //
  const [createOrUpdate, setCreateOrUpdate] = useState(false); //编辑框的disable
  const [form] = Form.useForm();
  const columns: ProColumns[] = [
    {
      title: '序号',
      dataIndex: 'index',
      valueType: 'index',
      width: 80,
      align: 'center',
      sorter: true,
    },
    {
      title: '类型名称',
      dataIndex: 'dictName',

      align: 'center',
      sorter: true,
    },
    {
      title: '类型编号',
      dataIndex: 'dictCode',

      align: 'center',
      sorter: true,
      render: (text, record) => [
        <NavLink
          to={{ pathname: '/systemSetting/dictData', state: { dictTypeId: record.id } }}
          key={record.id}
        >
          {text}
        </NavLink>,
      ],
    },

    {
      title: '备注',
      dataIndex: 'memo',
      align: 'center',
      sorter: true,
      search: false,
    },
    // {
    //   title: '预留',
    //   dataIndex: 'reserved',
    // },

    // {
    //   title: '创建日期', //表头显示的名称
    //   search: false,
    //   dataIndex: 'createTime',
    //
    //  align: 'center',sorter: true,
    // },
    // {
    //   title: '创建人', //表头显示的名称
    //   search: false,
    //   dataIndex: 'createName',
    //  align: 'center',sorter: true,
    // },
    {
      title: '操作',
      valueType: 'option',
      key: 'option',
      fixed: 'right',
      //
      align: 'center',
      sorter: true,
      render: (text, record, _, action) => [
        <a
          key="editKey"
          onClick={() => {
            setModalVisit(true);
            setCreateOrUpdate(true);
            setFormTitle('编辑');
            form.setFieldsValue({
              dictCode: record.dictCode,
              dictName: record.dictName,
              memo: record.memo,
              reserved: record.reserved,
              serialNo: record.serialNo,
              id: record.id,
            });
          }}
        >
          编辑
        </a>,
        <a
          key="deleteKey"
          onClick={() => {
            Modal.confirm({
              title: '删除',
              content: '确定要删除吗',
              okText: '确认',
              cancelText: '取消',
              onOk: async () => {
                await delDictType({ id: record.id });
                actionRef.current!.reload();
              },
            });
          }}
        >
          删除
        </a>,
      ],
    },
  ];
  const handleSubmit = async () => {
    setConfirmLoading(true);
    if (formTitle == '新建') {
      const result = await addDictType(form.getFieldsValue());
      if (result != null) {
        message.success('添加成功');
      }
    } else {
      await updateDictType(form.getFieldsValue());
    }
    setConfirmLoading(false);
    setModalVisit(false);
    actionRef.current!.reload();
  };

  return (
    <PageHeaderWrapper title={false} breadcrumb={undefined}>
      <ProTable
        headerTitle="字典类型查询"
        actionRef={actionRef} //用于触发刷新操作等，看api
        rowKey="id" //表格行 key 的取值，可以是字符串或一个函数
        revalidateOnFocus={false}
        toolBarRender={(action, { selectedRows }) => [
          <Button
            key="button"
            icon={<PlusOutlined />}
            onClick={() => {
              setCreateOrUpdate(false);
              setFormTitle('新建');
              form.resetFields();
              setModalVisit(true);
            }}
            type="primary"
          >
            新建
          </Button>,
        ]}
        request={async (params = {}, sort, filter) => {
          const sorters = sortTran(sort);
          const filters = filterTranNew(params);
          const result = await getDictTypeByPage({ ...params, sorters: sorters, filters: filters });
          console.log(result.data.content);
          return {
            data: result.data.content,
            total: result.data.totalElements,
            success: result.success,
          };
        }}
        columns={columns}
        scroll={{ x: 2000, y: 350 }}
        rowSelection={{}}
      />
      <Modal
        title={formTitle}
        open={modalVisit}
        onOk={handleSubmit}
        confirmLoading={confirmLoading}
        onCancel={() => setModalVisit(false)}
      >
        <Form
          name="wrap"
          labelCol={{ flex: '110px' }}
          labelAlign="left"
          labelWrap
          form={form}
          wrapperCol={{ flex: 1 }}
          colon={false}
        >
          <Form.Item label="id" name="id" hidden={true}>
            <Input />
          </Form.Item>
          <Form.Item
            label="类型名称"
            name="dictName"
            rules={[{ required: true, message: 'Please input your dictName!' }]}
          >
            <Input />
          </Form.Item>
          <Form.Item label="备注" name="memo">
            <Input />
          </Form.Item>
          <Form.Item label="预留" name="reserved">
            <Input />
          </Form.Item>
        </Form>
      </Modal>
    </PageHeaderWrapper>
  );
};
export default DictType;
